package com.lenovo.smarttraffic.zjh;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.util.Collections;
import java.util.List;

import static com.lenovo.smarttraffic.zjh._zjh_Activity_TrafficOnTime.list_Info;

public class _zjh_Activity_TrafficInfo extends BaseActivity {

    private TextView tv_StartEndName;
    private ImageView tvReverse;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private TextView tvAllLine;
    private TextView tvTickerPrice;
    private ListView lvLineInfo;
    private List<String> list_line = list_Info.get(0).getList_line();
    private Adapter_Sites adapter_sites;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(list_Info.get(0).getLine_Id() + "路");
        initView();

    }

    private void initView() {
        tv_StartEndName = (TextView) findViewById(R.id.tv_StartEndName);
        tvReverse = (ImageView) findViewById(R.id.tv_Reverse);
        tvStartTime = (TextView) findViewById(R.id.tv_StartTime);
        tvEndTime = (TextView) findViewById(R.id.tv_EndTime);
        tvAllLine = (TextView) findViewById(R.id.tv_AllLine);
        tvTickerPrice = (TextView) findViewById(R.id.tv_TickerPrice);
        lvLineInfo = (ListView) findViewById(R.id.lv_LineInfo);

        tv_StartEndName.setText(list_line.get(0) + "-" + list_line.get(list_line.size() - 1));
        tvStartTime.setText(list_Info.get(0).getStartTime());
        tvEndTime.setText(list_Info.get(0).getEndTime());
        tvAllLine.setText(list_line.size() + "站/" + (list_line.size() * 0.5) + "公里");
        tvTickerPrice.setText("最高票价" + list_line.size() * 0.2 + "元");

        adapter_sites = new Adapter_Sites();
        lvLineInfo.setAdapter(adapter_sites);

        lvLineInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter_sites.get_Position(position);
                adapter_sites.notifyDataSetChanged();
            }
        });

        tvReverse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collections.reverse(list_line);
                tv_StartEndName.setText(list_line.get(0) + "-" + list_line.get(list_line.size() - 1));
                adapter_sites.notifyDataSetChanged();
            }
        });

    }

    private class Adapter_Sites extends BaseAdapter {
        private TextView tvSiteName;
        private TextView tvID;
        private int pos = -20;

        @Override
        public int getCount() {
            return list_line.size();
        }

        @Override
        public Object getItem(int position) {
            return list_line.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(_zjh_Activity_TrafficInfo.this).inflate(R.layout._zjh_layout_sitesitem, parent, false);
            tvSiteName = (TextView) convertView.findViewById(R.id.tv_siteName);
            tvID = (TextView) convertView.findViewById(R.id.tv_ID);

            tvID.setText(position + 1 + "");
            tvSiteName.setText(list_line.get(position));

            if (position == pos) {
                tvID.setBackgroundResource(R.drawable._zjh_tv_circle_red);
                tvSiteName.setTextColor(0xFFD36161);
            } else {
                tvID.setBackgroundResource(R.drawable._zjh_tv_circle);
                tvSiteName.setTextColor(0xFF000000);
            }

//            convertView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    tvID.setBackgroundColor(Color.RED);
//                    tvSiteName.setTextColor(Color.RED);
//                }
//            });

            return convertView;
        }

        public void get_Position(int position) {
            this.pos = position;
            Log.e("getPosition", "get_Position: " + pos);
        }

    }


    @Override
    protected int getLayout() {
        return R.layout._zjh_layout_trafficinfo;
    }
}
